const fp = require('lodash/fp')
// 数据
// horsepower 马力，dollar_value 价格，in_stock 库存
const cars = [
  {
    name: 'Ferrari FF',
    horsepower: 660,
    in_stock: true,
    dollar_value: 700000
  },
  {
    name: 'Ferrari F1',
    horsepower: 662,
    in_stock: false,
    dollar_value: 700002
  },
  {
    name: 'Ferrari F2',
    horsepower: 663,
    in_stock: true,
    dollar_value: 700003
  },
  {
    name: 'Ferrari F4',
    horsepower: 664,
    in_stock: false,
    dollar_value: 700004
  },
  {
    name: 'Ferrari F5',
    horsepower: 665,
    in_stock: true,
    dollar_value: 700005
  },
]
// 练习一
// 使用函数组合 fp.flowRight() 重新实现下面这个函数
let isLastInStock = function (cars) {
  let last_car = fp.last(cars)
  return fp.prop('in_stock', last_car)
}
function last (cars) {
  return fp.last(cars)
}
function prop (last_car) {
  return fp.prop('in_stock', last_car)
}
let test1 = fp.flowRight([prop,last])
console.log(test1(cars))

// 练习二
function first (cars) {
  return fp.first(cars)
}
function prop (last_car) {
  return fp.prop('name', last_car)
}
let test2 = fp.flowRight([prop,first])

// 练习三
let _average = function (xs) {
  return fp.reduce(fp.add, 0, xs) / xs.length;
}
let dollar_values = function (cars) {
  return fp.map( (car) => {
    return car.dollar_value
  }, cars)
}
let averageDollarValue = fp.flow([fp.map(car => car.dollar_value),_average])

// 练习4
let _underscore = fp.replace(/\W+/g,'_')
function lowerCase(value) {
  console.log(value)
  return value.toLowerCase()
}
function sanitizeNames(cars){
   let fn = fp.flowRight([lowerCase,_underscore])
   return cars.map( item => {
     return fn(item.name)
   })
}